package org.schat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author lsh
 * @Date 2020/3/18 17:48
 **/
public class OnlineNumRunnable implements Runnable{
    @Override
    public void run() {
        while(true){
            pushOnlineClientList();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //查询 在线人数
    private void pushOnlineClientList(){
        Set<Map.Entry<String,ClientDomain>> clientSet =  IMServer.clientMap.entrySet();
        JSONObject clientListInfo = new JSONObject();
        clientListInfo.put("onlineNum",clientSet.size());
        JSONArray clientListArr = new JSONArray();
        for(Map.Entry<String,ClientDomain> entry : clientSet){
            ClientDomain clientDomain = entry.getValue();
            JSONObject  clientDetalInfo= new JSONObject();
            clientDetalInfo.put("clientId",clientDomain.getId());
            clientDetalInfo.put("clientName",clientDomain.getClientName());
            clientListArr.add(clientDetalInfo);
        }
        clientListInfo.put("clientList",clientListArr);
        IMServer.msgQue.offer(new MessageDomain("3",clientListInfo.toJSONString()));
    }
}
